569B - Inventory - CodeForces Solution


greedy math *1200

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <functional>
#include <numeric>
using namespace std;
const int MOD = 1000000007;
const int mod = 998244353;
// #define LOCAL
#ifdef LOCAL
#define dbg(x...) do { cout << "[" << #x <<" -> "; err(x); } while (0)
void err() { cout << "]" << endl; }
template<class T, class... Ts> void err(const T& arg,const Ts&... args) { cout << arg << " "; err(args...); }
#else 
#define dbg(x...){}
#endif
#define int long long
#define pp pair<int, int>
class Comb {
    vector<int> Facs, Invs;
    void expand(size_t n) {
        while(Facs.size() < n + 1) Facs.push_back(1ll * Facs.back() * Facs.size() % MOD);
        if(Invs.size() < n + 1) { // 线性求阶乘的逆元
            Invs.resize(n + 1, 0);
            Invs.back() = 1;
            for(int a = Facs[n], p = MOD - 2; p; p >>= 1, a = 1ll * a * a % MOD)
                if(p & 1) Invs.back() = 1ll * Invs.back() * a % MOD; // 快速乘求 n! 的逆元
            for(int j = n-1; !Invs[j]; --j) Invs[j] = 1ll * Invs[j+1] * (j + 1) % MOD;
        }
    }
public:
    Comb() : Facs({1}), Invs({1}) {}
    Comb(int n) : Facs({1}), Invs({1}) { expand(n); }
    int operator() (int n, int k) {
        if(n == 0) return 1;
        if(k > n) return 0;
        expand(n);
        return (1ll * Facs[n] * Invs[n-k] % MOD) * Invs[k] % MOD;
    }
};

int fac[20];

int quickpow(int base, int exponent) {
    // return fac[exponent ];
    int result = 1;
    base %= mod;
    while (exponent > 0) {
        dbg(exponent);
        if (exponent & 1) {
            result = (result * base) % mod;
        }
        exponent >>= 1;
        base = (base * base) % mod;
    }
    // dbg(result);
    return (result + mod) % mod;
}


Comb comb;

using ll = long long;
int cnt = 0;
const int N = 1e3+10;
vector<int> primes;
vector<bool> is_prime(N + 1, true);
auto init =[](){
    for(int i = 2; i < N; i++) {
        if(is_prime[i]) primes.push_back(i);
        for (int& j: primes) {
            if (i * j > N) {
                break;
            }
            is_prime[i * j] = false;
            if (i % j == 0) break;
        }
    }
};


bool F;

void solve(){
    int n;
    cin >> n;
    vector<int> arr(n);
    for(auto& x: arr) cin >> x;
    set<int> st;
    for(int i = 1; i <= n; i++) {
        st.insert(i);
    }
    for(int i = 0; i < n; i++) {
        if (st.count(arr[i])) st.erase(arr[i]);
    }
    set<int> his;
    for(int i = 0; i < n; i++) {
        if(his.count(arr[i]) || arr[i] > n) {
            cout << *st.begin() << " \n"[i == n - 1];
            st.erase(st.begin());
        }else{
            cout << arr[i] << " \n"[i == n - 1];
            his.insert(arr[i]);
        }
    }

}

int T = 0;

signed main(){
#ifndef LOCAL
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
#endif
    int t;
    // init();
    // cin >> t;
    int c = 0;
    // while(c++ < t) {
        solve();
    // }
    // cout << "done" << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II